# -*- Mode: Python -*- vi:si:et:sw=4:sts=4:ts=4:syntax=python


class Recipe(recipe.Recipe):
    name = 'cairo'
    version = '1.14.12'
    stype = SourceType.TARBALL
    url = 'https://cairographics.org/releases/cairo-%(version)s.tar.xz'
    tarball_checksum = '8c90f00c500b2299c0a323dd9beead2a00353752b2092ead558139bd67f7bf16'
    # Either LGPL-2.1+ or MPL-1.1
    licenses = [License.LGPLv2_1Plus, {License.MPLv1_1: ['COPYING-MPL-1.1']}]
    autoreconf = True
    autoreconf_sh = 'NOCONFIGURE=1 ./autogen.sh'
    deps = ['glib', 'libpng', 'zlib', 'pixman', 'fontconfig', 'freetype']
    patches = ['cairo/0001-Fix-compilation-with-Android-s-bionic-libc.patch',
               'cairo/0001-Disable-building-of-the-tests.patch',
               'cairo/0001-build-disable-failing-compile-link-checks-on-any-std.patch',
               ]

    files_libs = ['libcairo', 'libcairo-gobject', 'libcairo-script-interpreter']
    files_devel = [
        'lib/pkgconfig/cairo-fc.pc',
        'lib/pkgconfig/cairo.pc',
        'lib/pkgconfig/cairo-ps.pc',
        'lib/pkgconfig/cairo-ft.pc',
        'lib/pkgconfig/cairo-pdf.pc',
        'lib/pkgconfig/cairo-svg.pc',
        'lib/pkgconfig/cairo-gobject.pc',
        'lib/pkgconfig/cairo-png.pc',
        'include/cairo',
        ]
    platform_files_devel = {
        Platform.WINDOWS: [ 'lib/pkgconfig/cairo-win32-font.pc',
                            'lib/pkgconfig/cairo-win32.pc'],
        Platform.DARWIN: ['lib/pkgconfig/cairo-quartz-font.pc',
                         'lib/pkgconfig/cairo-quartz-image.pc',
                         'lib/pkgconfig/cairo-quartz.pc']
    }

    # Skipping because it requires a display and documented as randomly failing
    make_check = None

    def prepare(self):
        # extra flags needed for gcc 4.9
        if self.config.target_distro == Distro.ARCH:
            self.append_env('CFLAGS', '-fno-lto')
            self.append_env('CXXFLAGS', '-fno-lto')
            self.append_env('CPPFLAGS', '-fno-lto')

        if self.config.variants.x11:
            self.files_devel += ['lib/pkgconfig/cairo-xlib-xrender.pc',
                                 'lib/pkgconfig/cairo-xlib.pc']
        else:
            self.configure_options += '--without-x '
        if self.config.target_platform == Platform.DARWIN:
            self.configure_options += ' --enable-quartz-font --enable-quartz-image'
        elif self.config.target_platform == Platform.ANDROID:
            v = DistroVersion.get_android_api_version(self.config.target_distro_version)
            if self.config.target_arch in [Architecture.ARM, Architecture.ARMv7, Architecture.X86] and v < 21:
                # FIXME: HACK to make projects using mmap compile with NDK 16
                # we fail to compile with -D_FILE_OFFSET_BITS=64
                # because we don't use clang and we use a platform < 21 (Lollipop)
                # See $NDK_ROOT/sysroot/usr/include/sys/mman.h as one example
                self.set_env('ac_cv_func_mmap', 'no')
